home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 1 / ETO Development Tools 1.iso / Tools - Objects / C++ / MPW C++ 3.1b1 / Examples / CPlusExamples / StreamCounter.cp < prev    next >
Text File  |  1989-09-29  |  588b  |  40 lines

  1.  
  2. #include <StdLib.h>
  3. #include "StreamCounter.h"
  4.  
  5. static void
  6. error(char* e)
  7. {
  8.     cerr << "### StreamCounter Error: " << e << "\n";
  9.     exit(1);
  10. }
  11.  
  12. TStreamCounter::TStreamCounter(fstream* fs)
  13. {
  14.     if (fs == 0) fStream = (fstream*) &cin;
  15.     else fStream = fs;
  16.     fCount.chars = fCount.lines = 0;
  17. }
  18.  
  19. void
  20. TStreamCounter::Reset(fstream* fs)
  21. {
  22.     fStream = fs;
  23.     fCount.chars = fCount.lines = 0;
  24. }
  25.  
  26. void TStreamCounter::Count()
  27. {
  28.     int c, c1;
  29.     
  30.     while ((c = c1 = fStream->get()) != EOF) {
  31.         fCount.chars++;
  32.         if ( c == '\n' )
  33.             fCount.lines++;
  34.     }
  35.     
  36.     if (fCount.chars > 0 && c1 != '\n')
  37.         fCount.lines++;
  38. }
  39.  
  40.